[fix] race condition in NextRun() when multiple jobs complete simultaneously#907
Merged
JohnRoesler merged 6 commits intogo-co-op:v2from Jan 28, 2026
Merged
Conversation
JohnRoesler
reviewed
Jan 28, 2026
| // from nextScheduled after it completes. So all intervals should be 14 days | ||
| // (2 weeks as configured). | ||
| diff := time.Hour * 14 * 24 | ||
| if iteration == 1 { |
Contributor
There was a problem hiding this comment.
Lint is complaining that the iteration value passed in is unused now. Looks like it can be removed entirely from the assertion func
Contributor
Author
There was a problem hiding this comment.
Thanks for catching! Made quick fix here 0f55e2a based on feedback 🙏
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this do?
When multiple jobs completed at the same time,
NextRun()could return stale/incorrect values due to a race condition in thenextScheduledcleanup logic. The issue occurred because two different methods (selectExecJobsOutCompletedandupdateNextScheduled) were cleaning up thenextScheduledslice with different logic:!t.Before(now)(keeps times >= now)t.After(now)(keeps times > now)When these methods ran concurrently on different jobs, their writes could interleave and overwrite each other with inconsistent states.
Solution
Unified both methods to use identical cleanup logic:
t.After(now)(keep only strictly future times). This makes the cleanup idempotent - both methods now produce the same result regardless of execution order, eliminating the race condition.Which issue(s) does this PR fix/relate to?
selectExecJobsOutCompletedandupdateNextScheduledto use consistentt.After(now)logic for removing past scheduled timesTestJob_NextRun_MultipleJobsSimultaneouslyto reproduce and verify the fix for the original bugTestJob_NextRun_ConcurrentCompletionsto stress-test concurrent NextRun() queriesTestJob_NextRunsweekly job assertion - with proper cleanup, all intervals are now consistently 14 days (no special case for first run needed)Testing
All existing tests pass. New regression tests verify
NextRun()returns correct values when multiple jobs complete simultaneously.Notes